const puppeteer = require('puppeteer');

// (async () => {
//   const browser = await puppeteer.launch();
//   const page = await browser.newPage();
//   await page.goto('https://www.imooc.com');
//   await page.screenshot({path: `screenshot/${Date.now()}.png`});
//
//   await browser.close();
// })()
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://image.baidu.com');
  // await page.pdf({path: `pdf/${Date.now()}.pdf`, format: 'A4'});
  await page.setViewport({
    width: 1920,
    height: 1080
  })
  await page.focus('#kw')
  await page.keyboard.sendCharacter('')
  await page.click('.s_search')
  page.on('load', async(a) => {
    console.log('page loaded');
    console.log(a);
    const srcs = await page.evaluate(() => {
      const images=document.querySelectorAll('img.main_img')
      console.log(images.length);
      return Array.prototype.map.call(images,img=>img.src)
    });
    srcs.forEach(function (src) {
      console.log(src);
    })
    // const divsCounts = await page.$$eval('div', divs => divs.length);

    // const srcs=await page.$$eval('img.main_img', (imgs) =>{
    //   console.log(imgs.length);
    // } );
    // await browser.close();
  })

})();
/*
https://github.com/GoogleChrome/puppeteer

*/
